package com.many_to_many;


import com.bean.Course;
import com.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class test3 {
    @Test
   public void selectAll() throws Exception {
       InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

       SqlSession sqlSession = sqlSessionFactory.openSession(true);

       StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

       List<Student> list = mapper.selectALL();

       for (Student stu : list) {
           System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
           List<Course> courses = stu.getCourses();
           for (Course cours : courses) {
               System.out.println("\t" + cours);
           }
       }
       sqlSession.close();
       is.close();
   }
}
